﻿package view.component
{
    import __AS3__.vec.*;
    import base.component.*;
    import com.core.destroy.*;
    import com.sh.game.util.*;
    import flash.display.*;

    public class TimeCounter extends Object
    {
        private var _container:Sprite;
        private var _time:int = 0;
        private var _intvalID:uint = 0;
        private var _counts:Vector.<Bitmap>;
        private var _typeImg:Bitmap;
        private var _type:int = 0;
        private var W:int = 109;
        public var width:int = 138;
        private var _x:int = 0;
        private var _y:int = 0;

        public function TimeCounter(param1:Object)
        {
            this._container = Sprite(param1);
            this.init();
            return;
        }// end function

        public function get y() : int
        {
            return this._y;
        }// end function

        public function set y(param1:int) : void
        {
            var _loc_2:Bitmap = null;
            this._y = param1;
            if (this._typeImg)
            {
                this._typeImg.y = param1;
            }
            if (this._counts)
            {
                for each (_loc_2 in this._counts)
                {
                    
                    _loc_2.y = param1;
                }
            }
            return;
        }// end function

        public function get x() : int
        {
            return this._x;
        }// end function

        public function set x(param1:int) : void
        {
            var _loc_2:int = 0;
            var _loc_3:Bitmap = null;
            this._x = param1;
            if (this._typeImg)
            {
                this._typeImg.x = param1;
            }
            if (this._counts)
            {
                _loc_2 = 0;
                for each (_loc_3 in this._counts)
                {
                    
                    _loc_3.x = param1 + this.W + _loc_2;
                    _loc_2 = _loc_3.width;
                }
            }
            return;
        }// end function

        private function init() : void
        {
            return;
        }// end function

        private function clearNum() : void
        {
            var _loc_1:Bitmap = null;
            for each (_loc_1 in this._counts)
            {
                
                if (_loc_1)
                {
                    if (_loc_1.parent)
                    {
                        _loc_1.parent.removeChild(_loc_1);
                    }
                    if (_loc_1.bitmapData)
                    {
                        _loc_1.bitmapData = null;
                    }
                }
            }
            DestroyUtil.destroyVector(this._counts);
            return;
        }// end function

        public function update(param1:int, param2:int) : void
        {
            var _loc_3:BitmapData = null;
            this._time = param2;
            if (this._type != param1)
            {
                this._type = param1;
                if (!this._typeImg)
                {
                    _loc_3 = App.asset.getBitmapData("png.common.timeout" + param1);
                    this._typeImg = new Bitmap(_loc_3);
                    this._container.addChildAt(this._typeImg, 0);
                    this._typeImg.x = this.x;
                    this._typeImg.y = this.y;
                }
                else
                {
                    _loc_3 = App.asset.getBitmapData("png.common.timeout" + param1);
                    this._typeImg.bitmapData = _loc_3;
                    if (!this._typeImg.parent)
                    {
                        this._container.addChildAt(this._typeImg, 0);
                    }
                }
            }
            this.W = this._typeImg.width;
            this.changeNum();
            if (this._intvalID == 0)
            {
                this._intvalID = SecondTimerUtil.instance.addSecondTask(this.timerHandler);
            }
            return;
        }// end function

        private function changeNum() : void
        {
            this.clearNum();
            this._counts = ImageNumber.addNumber("pow2", this._time + "", this._container, this.x + this.W, this.y);
            return;
        }// end function

        private function clearTimer() : void
        {
            if (this._intvalID > 0)
            {
                SecondTimerUtil.instance.removeTask(this._intvalID);
            }
            this._intvalID = 0;
            return;
        }// end function

        private function timerHandler() : void
        {
            if (this._time > 0)
            {
                var _loc_1:String = this;
                var _loc_2:* = this._time - 1;
                _loc_1._time = _loc_2;
                this.changeNum();
            }
            else
            {
                this.clear();
            }
            return;
        }// end function

        public function clear(param1:int = 0) : void
        {
            if (param1 == 0 || param1 == this._type)
            {
                this.clearTimer();
                this._type = 0;
                this.clearNum();
                if (this._typeImg && this._typeImg.parent)
                {
                    this._typeImg.parent.removeChild(this._typeImg);
                    if (this._typeImg.bitmapData)
                    {
                        this._typeImg.bitmapData = null;
                    }
                    this._typeImg = null;
                }
            }
            return;
        }// end function

    }
}
